package com.pan.server;

import com.pan.frame.ChatFrame;
import com.pan.frame.ConnectFrame;
import com.pan.frame.WaitConnectFrame;
import com.pan.frame.other.TipsFrame;
import com.pan.net.Communication;
import com.pan.net.Connection;
import com.pan.utils.ErrorLog;

import java.io.IOException;
import java.net.Socket;

public class MyServer {


    private Connection connection;
    private Communication communication;

    private ChatFrame chatFrame;
    private ConnectFrame connectFrame;
    private WaitConnectFrame waitConnectFrame;

    private String name = "";

    public void setName(String name) {
        this.name = name;
    }

    public MyServer() {
        connection = new Connection();
        connectFrame = new ConnectFrame(this);

    }

    public void init(){
        connectFrame.init();
    }


    public void connect( String host, int port, String password){
        connectFrame.setEnable(false);
        boolean isConnection = connection.connect(host, port, password);

        if (isConnection){
            ErrorLog.print("Server.connect",isConnection+"");
            Socket s = connection.getSocket();
            try {
                communication = new Communication(s);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            new Thread(()->{
                new TipsFrame("连接成功","成功连接"+s.getInetAddress());
            }).start();

            connectFrame.close();
            //成功
            chatFrame = new ChatFrame(communication,name);
            chatFrame.init();

        }else {
            //失败
            new Thread(()->{
                new TipsFrame("连接失败","");
            }).start();
            connectFrame.setEnable(true);
        }
    }

    public void waitConnect(String password){
        connectFrame.setEnable(false);
        try {
            waitConnectFrame = new WaitConnectFrame(this,password);
            waitConnectFrame.init();
            connection.waitConnect(password);
            monitorWait();
        } catch (IOException e) {
            e.printStackTrace();
            new Thread(()->{
                new TipsFrame("错误",e.getMessage());
            }).start();
        }
    }
//////////////////////////////////

    public void monitorWait()  {
        new Thread(()->{
            boolean b = connection.checkSocket();
            ErrorLog.print("Server.monitorWait",b+"");
            if (b){

                waitConnectFrame.close();
                Socket s = connection.getSocket();
                try {
                    communication = new Communication(s);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                new Thread(()->{
                    new TipsFrame("连接成功",s.getInetAddress()+" 同你建立了连接");
                }).start();
                chatFrame = new ChatFrame(communication,name);
                chatFrame.init();
            }
        }).start();
    }
    public void quitWait(){
        connection.offWaitConnection();
        connection.offCheckSocket();
        waitConnectFrame.close();
        connectFrame.setEnable(true);
    }




}
